package com.elective.courseselect.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elective.courseselect.dao.CourseQuestionnaireDao;
import com.elective.courseselect.entity.CourseQuestionnaireEntity;
import com.elective.courseselect.entity.QuestionEntity;
import com.elective.courseselect.entity.QuestionnaireEntity;
import com.elective.courseselect.enums.QuestionnaireType;
import com.elective.courseselect.service.CharacterService;
import com.elective.courseselect.service.CourseQuestionnaireService;
import com.elective.courseselect.service.QuestionService;
import com.elective.courseselect.service.QuestionnaireService;
import com.elective.courseselect.utils.PageUtils;
import com.elective.courseselect.utils.Query;
import com.elective.courseselect.utils.R;
import com.elective.status.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Map;



@Service("courseQuestionnaireService")
public class CourseQuestionnaireServiceImpl extends ServiceImpl<CourseQuestionnaireDao, CourseQuestionnaireEntity> implements CourseQuestionnaireService {


    @Autowired
    QuestionService questionService;

    @Autowired
    QuestionnaireService questionnaireService;

    @Autowired
    CharacterService characterService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CourseQuestionnaireEntity> page = this.page(
                new Query<CourseQuestionnaireEntity>().getPage(params),
                new QueryWrapper<CourseQuestionnaireEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<QuestionEntity> searchClassQuestion(Integer classId) {
        QueryWrapper<CourseQuestionnaireEntity> courseQuestionnaireQuery = new QueryWrapper<>();
        courseQuestionnaireQuery.eq("course_id", classId);
        CourseQuestionnaireEntity courseQuestionnaireEntity = this.getOne(courseQuestionnaireQuery);
        if (null == courseQuestionnaireEntity) {
            throw new RuntimeException("course_questionnaire 表不存在 当前课程id" + classId);
        }
        Integer questionnaireId = courseQuestionnaireEntity.getQuestionnaireId();
        QueryWrapper<QuestionEntity> questionEntityQuery = new QueryWrapper<>();
        questionEntityQuery.eq("questionnaire_id", questionnaireId);
        List<QuestionEntity> list = questionService.list(questionEntityQuery);
        for (QuestionEntity questionEntity : list) {
            questionEntity.setOptionByJson(JSONObject.parseArray(questionEntity.getOption()));
            questionEntity.setReferenceByJson(JSONObject.parseArray(questionEntity.getReference()));
        }
        return list;
    }

    @Override
    public R searchCharacterQuestion(Integer questionnaireId) {
        QuestionnaireEntity byId = questionnaireService.getById(questionnaireId);
        if (byId.getType() != QuestionnaireType.PERSONALITY_QUESTIONNAIRE) {
            return R.error(StatusCode.ERROR, "当前传入的不是性格调查问卷id");
        }
        QueryWrapper<QuestionEntity> questionEntityQuery = new QueryWrapper<>();
        questionEntityQuery.eq("questionnaire_id", questionnaireId);
        List<QuestionEntity> list = questionService.list(questionEntityQuery);
        for (QuestionEntity questionEntity : list) {
            questionEntity.setOptionByJson(JSONObject.parseArray(questionEntity.getOption()));
            questionEntity.setReferenceByJson(JSONObject.parseArray(questionEntity.getReference()));
            HashSet<String> strings = new HashSet<>();
            if (questionEntity.getCharactersId() != null && questionEntity.getCharactersId().split(",") != null && !questionEntity.getCharactersId().equals("")) {
                for (String characterId : questionEntity.getCharactersId().split(",")) {
                    strings.add(characterService.getById(characterId).getLabel());
                }
            }
            questionEntity.setCharactersNames(strings);
        }
        return R.ok().put("list", list);
    }
}